<?php
/**
 * Created by PhpStorm.
 * User: nightelf
 * Date: 2020/5/29
 * Time: 10:56
 */

namespace App\Http\Model;


use Illuminate\Support\Facades\DB;

class LivingBookModel extends Model
{
    const TABLE = 'living_book';

    public function lists($param)
    {
        return DB::table(self::TABLE)
            ->leftJoin('admin as a','a.id','=',self::TABLE.'.adminId')
            ->where(function ($query) use ($param) {
                if (!empty($param['projectId'])) {
                    $query->where(self::TABLE . '.projectId', $param['projectId']);
                }
                if (!empty($param['type'])) {
                    $query->where(self::TABLE . '.type', $param['type']);
                }
                if (isset($param['search']) && $param['search'] != '') {
                    $query->where(self::TABLE.'.name', 'like', '%' . $param['search'] . '%');
                }
                if (isset($param['startTime']) && !is_null($param['startTime'])) {
                    $query->where('createTime', '>=', $param['startTime'] . " 00:00:00");
                }
                if (isset($param['endTime']) && !is_null($param['endTime'])) {
                    $query->where('createTime', '<=', $param['endTime'] . " 23:59:59");
                }
            })
            ->orderBy('createTime', 'desc')
            ->select(self::TABLE.'.*','a.name as adminName')
            ->get()->toArray();
    }

    public function countLists(array $param)
    {
        return DB::table(self::TABLE)
            ->where(function ($query) use ($param) {
                if (!empty($param['projectId'])) {
                    $query->where(self::TABLE . '.projectId', $param['projectId']);
                }
                if (!empty($param['type'])) {
                    $query->where(self::TABLE . '.type', $param['type']);
                }
                if (isset($param['search']) && $param['search'] != '') {
                    $query->where(self::TABLE.'.name', 'like', '%' . $param['search'] . '%');
                }

                if (isset($param['startTime']) && !is_null($param['startTime'])) {
                    $query->where('createTime', '>=', $param['startTime'] . " 00:00:00");
                }
                if (isset($param['endTime']) && !is_null($param['endTime'])) {
                    $query->where('createTime', '<=', $param['endTime'] . " 23:59:59");
                }
            })->count();
    }

    public function info(array $param)
    {
        $result = DB::table(self::TABLE)->where($param)->first();
        return empty($result)?[]:get_object_vars($result);
    }

    public function insert(array $data)
    {
        $data['createTime'] = date('Y-m-d H:i:s');
        $data['status'] = 2;
        return DB::table(self::TABLE)->insertGetId($data);
    }

    public function delete(array $data)
    {
        return DB::table(self::TABLE)->where($data)->delete();
    }

    public function update(array $where, array $data)
    {
        return DB::table(self::TABLE)->where($where)->update($data);
    }

    /**
     * @param array $data
     * @return mixed
     */
    public function getSum(array $data)
    {
        return DB::table(self::TABLE)
            ->where(function ($query) use ($data) {
                if (!empty($data['projectId'])) {
                    $query->where(self::TABLE . '.projectId', $data['projectId']);
                }
                if (!empty($data['type'])) {
                    $query->where(self::TABLE . '.type', $data['type']);
                }
            })->sum('account');
    }
}